import cv2
import os
import numpy as np
import copy
import random
def add_noise(img,k=5):
    new_img = copy.deepcopy(img)
    for i in range(k): #添加点噪声
        temp_x = np.random.randint(0,new_img.shape[0])
        temp_y = np.random.randint(0,new_img.shape[1])
        new_img[temp_x][temp_y] = 255
    return new_img

def remove_noise(img,k=5):
    new_img = copy.deepcopy(img)
    for i in range(k): #添加点噪声
        temp_x = np.random.randint(0,new_img.shape[0])
        temp_y = np.random.randint(0,new_img.shape[1])
        new_img[temp_x][temp_y] = 0
    return new_img
def add_v(src1,length, top=True):


    s1 = np.asarray(src1, dtype=np.uint8).tolist()

    if top:
        new_s1 = []

        for k in range(len(s1)):
            new_s1.append(s1[k])
        for k in range(length):
            t_l = []
            for i in range(len(s1[0])):
                t_l.append(0)
            new_s1.append(t_l)

    else :
        new_s1 = []

        for k in range(length):
            t_l = []
            for i in range(len(s1[0])):
                t_l.append(0)
            new_s1.append(t_l)

        for k in range(len(s1)):
            new_s1.append(s1[k])


    return np.asarray(new_s1, dtype=np.uint8)

    # test_images_dir = save_path.replace('train', 'test')
    # train_images_dir = save_path
    # np.random.shuffle(all_imgs)
    # test_num = len(all_imgs) * 0.2
    # count = 0
    # for i in range(len(all_imgs)):
    #
    #     if count < test_num :
    #         char_dir = os.path.join(test_images_dir, str(new_start))
    #     else:
    #         char_dir = os.path.join(train_images_dir, str(new_start))
    #
    #     if not os.path.isdir(char_dir):
    #         os.makedirs(char_dir)
    #
    #     path_image = os.path.join(char_dir, "%d.png" % count)
    #     cv2.imwrite(path_image, all_imgs[i])
    #     count +=1

def concat(src1,src2):  #水平连接
    img = []

    for s1,s2 in zip(src1,src2):
        t = []
        for i in s1:
            t.append(i)
        for i in s2:
            t.append(i)
        img.append(t)
    return np.asarray(img,dtype=np.uint8)


def concat_img(src1,src2,std_diff=1):
    diff = src1.shape[0]- src2.shape[0]
    s1 = np.asarray(src1,dtype=np.uint8).tolist()
    s2 = np.asarray(src2,dtype=np.uint8).tolist()

    if diff == 0:
        return concat(src1,src2)
    elif diff >0:
        new_s2 = []
        for k in range(diff - std_diff +1):
            t_l = []
            for i in range(len(s2[0])):
                t_l.append(0)
            new_s2.append(t_l)

        for k in range(len(s2)):
            new_s2.append(s2[k])

        for k in range(std_diff -1):
            t_l = []
            for i in range(len(s2[0])):
                t_l.append(0)
            new_s2.append(t_l)


        new_img = np.asarray(new_s2, dtype=np.uint8)
        return concat(s1, new_img)
    else:
        new_s1 = []
        diff = 0 - diff
        for k in range(diff - std_diff +1):
            t_l = []
            for i in range(len(s1[0])):
                t_l.append(0)
            new_s1.append(t_l)

        for k in range(len(s1)):
            new_s1.append(s1[k])

        for k in range(std_diff -1):
            t_l = []
            for i in range(len(s1[0])):
                t_l.append(0)
            new_s1.append(t_l)

        new_img = np.asarray(new_s1, dtype=np.uint8)
        return concat(new_img, s2)
def dilate_one_img(img_path):
    dir_name = img_path.split('.')[0]
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)

        img = cv2.imread(img_path, 0)
        cv2.imwrite(os.path.join(dir_name,  '2a.png'), img)

    std_l = [[0,0],[0,1],[1,0],[1,1]]
    for i in range(4):
        new_img = add_v(img, std_l[i][0], False)
        save_img = add_v(new_img, std_l[i][1], True)

        # si = add_noise(save_img, 5)
        # cv2.imwrite(os.path.join(dir_name, str(i) + '_2a.png'), si)
        # si = remove_noise(save_img, 5)
        # cv2.imwrite(os.path.join(dir_name, str(i) + '_2r.png'), si)

        si = add_noise(save_img, 4)
        cv2.imwrite(os.path.join(dir_name, str(i) + '_1a.png'), si)
        si = remove_noise(save_img, 4)
        cv2.imwrite(os.path.join(dir_name, str(i) + '_1r.png'), si)

        for k in range(2):
            new_i = concat_img(save_img, np.zeros((save_img.shape[0], 1), dtype=np.uint8))

            cv2.imwrite(os.path.join(dir_name, str(i) + str(k) + '1as.png'), new_i)

            si = add_noise(new_i, 3)
            cv2.imwrite(os.path.join(dir_name, str(i) + str(k) + '_1a.png'), si)
            si = remove_noise(new_i, 3)
            cv2.imwrite(os.path.join(dir_name, str(i) + str(k) + '_1r.png'), si)

            si = add_noise(new_i, 4)
            cv2.imwrite(os.path.join(dir_name, str(i) + str(k) + '_11a.png'), si)
            si = remove_noise(new_i, 5)
            cv2.imwrite(os.path.join(dir_name, str(i) + str(k) + '_11r.png'), si)

            # si = add_noise(new_i, 5)
            # cv2.imwrite(os.path.join(dir_name, str(i) + str(k) + '_3a.png'), si)
            # si = remove_noise(new_i, 6)
            # cv2.imwrite(os.path.join(dir_name, str(i) + str(k) + '_3r.png'), si)
            #
            # si = add_noise(new_i, 7)
            # cv2.imwrite(os.path.join(dir_name, str(i) + str(k) + '_14a.png'), si)
            # si = remove_noise(new_i, 7)
            # cv2.imwrite(os.path.join(dir_name, str(i) + str(k) + '_14r.png'), si)

def dilate_one_img_to_dir(img_path, name0, name1, save_path):    #将所有图片生成到一个文件夹下

    if not os.path.exists(save_path):
        os.makedirs(save_path)

    img = cv2.imread(img_path, 0)
    cv2.imwrite(os.path.join(save_path,  name0 + '_' + name1 + '_00.png'), img)
    for i in range(0,1):
        new_img = add_v(img, i, False)
        save_img = add_v(new_img, i, True)

        si = add_noise(save_img, 3)
        cv2.imwrite(os.path.join(save_path,  name0 + '_' + name1 +'_'+ str(i) + '_2l.png'), si)
        si = remove_noise(save_img, 3)
        cv2.imwrite(os.path.join(save_path,  name0 + '_' + name1 +'_'+ str(i) + '_2r.png'), si)

        # si = add_noise(save_img, 4)
        # cv2.imwrite(os.path.join(save_path,  name0 + '_' + name1 +'_'+ str(i) + '_1a.png'), si)
        # si = remove_noise(save_img, 3)
        # cv2.imwrite(os.path.join(save_path,  name0 + '_' + name1 +'_'+ str(i) + '_1r.png'), si)

        # for k in range(2):
        #     new_i = concat_img(save_img, np.zeros((save_img.shape[0], 1), dtype=np.uint8))
        #
        #     cv2.imwrite(os.path.join(save_path,  name0 + '_' + name1 +'_'+ str(i) + str(k) + '_bb.png'), new_i)
        #
        #     si = add_noise(new_i, 4)
        #     cv2.imwrite(os.path.join(save_path,  name0 + '_' + name1 +'_'+ str(i) + str(k) + '_1a.png'), si)
        #     si = remove_noise(new_i, 4)
        #     cv2.imwrite(os.path.join(save_path,  name0 + '_' + name1 +'_'+ str(i) + str(k) + '_1r.png'), si)
        #
        #     si = add_noise(new_i, 3)
        #     cv2.imwrite(os.path.join(save_path,  name0 + '_' + name1 +'_'+ str(i) + str(k) + '_5a.png'), si)
        #     si = remove_noise(new_i, 3)
        #     cv2.imwrite(os.path.join(save_path,  name0 + '_' + name1 +'_'+ str(i) + str(k) + '_5r.png'), si)
if __name__ == '__main__':

    path = r'C:\Users\vanlance\Desktop\char\save\1'
    new_path = r'C:\Users\vanlance\Desktop\char\save\char\2'
    # make_dir(0, 8, new_path)
    # start = 0
    # end = 29
    # new_start = 4500
    # height = 35
    # count = 0

    # path0 = r'C:\Users\vanlance\Desktop\char\width_large_height\0'
    # new_path = r'C:\Users\vanlance\Desktop\char\width_large_height\2'
    # # for d in os.listdir(path):
    # #     path0 = os.path.join(path,d)
    # i = 0
    # for p in os.listdir(path0):
    #     path_ = os.path.join(path0, p)
    #     if os.path.isfile(path_):
    #         dilate_one_img_to_dir(path_, str(i), p, new_path)
    #         i +=1

    path = r'C:\Users\vanlance\Desktop\char\with'
    for d in os.listdir(path):
        path0 = os.path.join(path,d)

        if os.path.isfile(path0):
            dilate_one_img(path0)



